#ifndef THINK_THINK_LOG_DRIVER_FILE_H
#define THINK_THINK_LOG_DRIVER_FILE_H

static char* think_log_driver_file =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\log\\driver;\n"
	"\n"
	"use think\\App;\n"
	"use think\\Request;\n"
	"\n"
	"/**\n"
	" * 本地化调试输出到文件\n"
	" */\n"
	"class File\n"
	"{\n"
	"    protected $config = [\n"
	"        \'time_format\' => \' c \',\n"
	"        \'single\'      => false,\n"
	"        \'file_size\'   => 2097152,\n"
	"        \'path\'        => LOG_PATH,\n"
	"        \'apart_level\' => [],\n"
	"        \'max_files\'   => 0,\n"
	"        \'json\'        => false,\n"
	"    ];\n"
	"\n"
	"    // 实例化并传入参数\n"
	"    public function __construct($config = [])\n"
	"    {\n"
	"        if (is_array($config)) {\n"
	"            $this->config = array_merge($this->config, $config);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 日志写入接口\n"
	"     * @access public\n"
	"     * @param  array    $log 日志信息\n"
	"     * @param  bool     $append 是否追加请求信息\n"
	"     * @return bool\n"
	"     */\n"
	"    public function save(array $log = [], $append = false)\n"
	"    {\n"
	"        $destination = $this->getMasterLogFile();\n"
	"\n"
	"        $path = dirname($destination);\n"
	"        !is_dir($path) && mkdir($path, 0755, true);\n"
	"\n"
	"        $info = [];\n"
	"        foreach ($log as $type => $val) {\n"
	"\n"
	"            foreach ($val as $msg) {\n"
	"                if (!is_string($msg)) {\n"
	"                    $msg = var_export($msg, true);\n"
	"                }\n"
	"\n"
	"                $info[$type][] = $this->config[\'json\'] ? $msg : \'[ \' . $type . \' ] \' . $msg;\n"
	"            }\n"
	"\n"
	"            if (!$this->config[\'json\'] && (true === $this->config[\'apart_level\'] || in_array($type, $this->config[\'apart_level\']))) {\n"
	"                // 独立记录的日志级别\n"
	"                $filename = $this->getApartLevelFile($path, $type);\n"
	"\n"
	"                $this->write($info[$type], $filename, true, $append);\n"
	"                unset($info[$type]);\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if ($info) {\n"
	"            return $this->write($info, $destination, false, $append);\n"
	"        }\n"
	"\n"
	"        return true;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取主日志文件名\n"
	"     * @access public\n"
	"     * @return string\n"
	"     */\n"
	"    protected function getMasterLogFile()\n"
	"    {\n"
	"        if ($this->config[\'single\']) {\n"
	"            $name = is_string($this->config[\'single\']) ? $this->config[\'single\'] : \'single\';\n"
	"\n"
	"            $destination = $this->config[\'path\'] . $name . \'.log\';\n"
	"        } else {\n"
	"            $cli = PHP_SAPI == \'cli\' ? \'_cli\' : \'\';\n"
	"\n"
	"            if ($this->config[\'max_files\']) {\n"
	"                $filename = date(\'Ymd\') . $cli . \'.log\';\n"
	"                $files    = glob($this->config[\'path\'] . \'*.log\');\n"
	"\n"
	"                try {\n"
	"                    if (count($files) > $this->config[\'max_files\']) {\n"
	"                        unlink($files[0]);\n"
	"                    }\n"
	"                } catch (\\Exception $e) {\n"
	"                }\n"
	"            } else {\n"
	"                $filename = date(\'Ym\') . DIRECTORY_SEPARATOR . date(\'d\') . $cli . \'.log\';\n"
	"            }\n"
	"\n"
	"            $destination = $this->config[\'path\'] . $filename;\n"
	"        }\n"
	"\n"
	"        return $destination;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取独立日志文件名\n"
	"     * @access public\n"
	"     * @param  string $path 日志目录\n"
	"     * @param  string $type 日志类型\n"
	"     * @return string\n"
	"     */\n"
	"    protected function getApartLevelFile($path, $type)\n"
	"    {\n"
	"        $cli = PHP_SAPI == \'cli\' ? \'_cli\' : \'\';\n"
	"\n"
	"        if ($this->config[\'single\']) {\n"
	"            $name = is_string($this->config[\'single\']) ? $this->config[\'single\'] : \'single\';\n"
	"\n"
	"            $name .= \'_\' . $type;\n"
	"        } elseif ($this->config[\'max_files\']) {\n"
	"            $name = date(\'Ymd\') . \'_\' . $type . $cli;\n"
	"        } else {\n"
	"            $name = date(\'d\') . \'_\' . $type . $cli;\n"
	"        }\n"
	"\n"
	"        return $path . DIRECTORY_SEPARATOR . $name . \'.log\';\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 日志写入\n"
	"     * @access protected\n"
	"     * @param  array     $message 日志信息\n"
	"     * @param  string    $destination 日志文件\n"
	"     * @param  bool      $apart 是否独立文件写入\n"
	"     * @param  bool      $append 是否追加请求信息\n"
	"     * @return bool\n"
	"     */\n"
	"    protected function write($message, $destination, $apart = false, $append = false)\n"
	"    {\n"
	"        // 检测日志文件大小，超过配置大小则备份日志文件重新生成\n"
	"        $this->checkLogSize($destination);\n"
	"\n"
	"        // 日志信息封装\n"
	"        $info[\'timestamp\'] = date($this->config[\'time_format\']);\n"
	"\n"
	"        foreach ($message as $type => $msg) {\n"
	"            $info[$type] = is_array($msg) ? implode(\"\\r\\n\", $msg) : $msg;\n"
	"        }\n"
	"\n"
	"        if (PHP_SAPI == \'cli\') {\n"
	"            $message = $this->parseCliLog($info);\n"
	"        } else {\n"
	"            // 添加调试日志\n"
	"            $this->getDebugLog($info, $append, $apart);\n"
	"\n"
	"            $message = $this->parseLog($info);\n"
	"        }\n"
	"\n"
	"        return error_log($message, 3, $destination);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 检查日志文件大小并自动生成备份文件\n"
	"     * @access protected\n"
	"     * @param  string    $destination 日志文件\n"
	"     * @return void\n"
	"     */\n"
	"    protected function checkLogSize($destination)\n"
	"    {\n"
	"        if (is_file($destination) && floor($this->config[\'file_size\']) <= filesize($destination)) {\n"
	"            try {\n"
	"                rename($destination, dirname($destination) . DIRECTORY_SEPARATOR . time() . \'-\' . basename($destination));\n"
	"            } catch (\\Exception $e) {\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * CLI日志解析\n"
	"     * @access protected\n"
	"     * @param  array     $info 日志信息\n"
	"     * @return string\n"
	"     */\n"
	"    protected function parseCliLog($info)\n"
	"    {\n"
	"        if ($this->config[\'json\']) {\n"
	"            $message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . \"\\r\\n\";\n"
	"        } else {\n"
	"            $now = $info[\'timestamp\'];\n"
	"            unset($info[\'timestamp\']);\n"
	"\n"
	"            $message = implode(\"\\r\\n\", $info);\n"
	"\n"
	"            $message = \"[{$now}]\" . $message . \"\\r\\n\";\n"
	"        }\n"
	"\n"
	"        return $message;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 解析日志\n"
	"     * @access protected\n"
	"     * @param  array     $info 日志信息\n"
	"     * @return string\n"
	"     */\n"
	"    protected function parseLog($info)\n"
	"    {\n"
	"        $request     = Request::instance();\n"
	"        $requestInfo = [\n"
	"            \'ip\'     => $request->ip(),\n"
	"            \'method\' => $request->method(),\n"
	"            \'host\'   => $request->host(),\n"
	"            \'uri\'    => $request->url(),\n"
	"        ];\n"
	"\n"
	"        if ($this->config[\'json\']) {\n"
	"            $info = $requestInfo + $info;\n"
	"            return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . \"\\r\\n\";\n"
	"        }\n"
	"\n"
	"        array_unshift($info, \"---------------------------------------------------------------\\r\\n[{$info[\'timestamp\']}] {$requestInfo[\'ip\']} {$requestInfo[\'method\']} {$requestInfo[\'host\']}{$requestInfo[\'uri\']}\");\n"
	"        unset($info[\'timestamp\']);\n"
	"\n"
	"        return implode(\"\\r\\n\", $info) . \"\\r\\n\";\n"
	"    }\n"
	"\n"
	"    protected function getDebugLog(&$info, $append, $apart)\n"
	"    {\n"
	"        if (App::$debug && $append) {\n"
	"\n"
	"            if ($this->config[\'json\']) {\n"
	"                // 获取基本信息\n"
	"                $runtime = round(microtime(true) - THINK_START_TIME, 10);\n"
	"                $reqs    = $runtime > 0 ? number_format(1 / $runtime, 2) : \'∞\';\n"
	"\n"
	"                $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);\n"
	"\n"
	"                $info = [\n"
	"                    \'runtime\' => number_format($runtime, 6) . \'s\',\n"
	"                    \'reqs\'    => $reqs . \'req/s\',\n"
	"                    \'memory\'  => $memory_use . \'kb\',\n"
	"                    \'file\'    => count(get_included_files()),\n"
	"                ] + $info;\n"
	"\n"
	"            } elseif (!$apart) {\n"
	"                // 增加额外的调试信息\n"
	"                $runtime = round(microtime(true) - THINK_START_TIME, 10);\n"
	"                $reqs    = $runtime > 0 ? number_format(1 / $runtime, 2) : \'∞\';\n"
	"\n"
	"                $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);\n"
	"\n"
	"                $time_str   = \'[运行时间：\' . number_format($runtime, 6) . \'s] [吞吐率：\' . $reqs . \'req/s]\';\n"
	"                $memory_str = \' [内存消耗：\' . $memory_use . \'kb]\';\n"
	"                $file_load  = \' [文件加载：\' . count(get_included_files()) . \']\';\n"
	"\n"
	"                array_unshift($info, $time_str . $memory_str . $file_load);\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif